package com.study.debby.smarttable

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.widget.Toast
import com.bin.david.form.data.format.draw.ImageResDrawFormat
import com.bin.david.form.data.style.LineStyle
import com.bin.david.form.data.table.ArrayTableData
import com.bin.david.form.data.table.TableData
import com.study.debby.smarttable.dagger.DaggerComponent
import com.study.debby.smarttable.dagger.Format
import kotlinx.android.synthetic.main.activity_movie.*
import javax.inject.Inject

/**
 * Created by debby on 2018/6/8.
 * 描述:
 */
class MovieSelectActivity : BaseActivity() {
    val activity = this

    @Inject
    lateinit var format: Format

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initTitleBar(true)
        setContentView(R.layout.activity_movie)
        DaggerComponent.create().init(this)
        initView()
    }

    private fun initView() {
        val info = arrayOf(arrayOf(null, null, 2, 1, 1, 0, 1, 1, 0, 1, 1, null),
                arrayOf(4, 2, 1, 1, 0, 1, 1, 0, 1, 1, 2, 2),
                arrayOf(2, 2, 0, 1, 2, 4, 1, 0, 1, 3, 0, 1),
                arrayOf(2, 1, 1, 0, 1, 4, 0, 1, 1, 2, 2, 0),
                arrayOf(0, 1, 2, 4, 1, 0, 1, 4, 0, 1, 1, 2),
                arrayOf(1, 0, 1, 3, 2, 2, 0, 1, 2, 1, 1, 0),
                arrayOf(null, null, 2, 4, 0, 1, 2, 1, 1, 0, 1, null))
        val tableData = ArrayTableData.create(movie_table, "", info
                , object : ImageResDrawFormat<Int>(64, 64) {
            override fun getResourceID(status: Int?, value: String?, position: Int): Int {
                if (status == null) {
                    return 0
                }
                return if (status == 0) R.mipmap.ic_unselect
                else R.mipmap.ic_selected
            }

            override fun getContext(): Context {
                return activity
            }

        })
        tableData.onItemClickListener = TableData.OnItemClickListener<Int> { _, value, select, col, row ->
            if (select != null) {
                Toast.makeText(activity,
                        "列：$col 行：$row 数据：$value 创建时间：${format.getToday()}"
                        , Toast.LENGTH_SHORT).show()
                tableData.data[col][row] = when (select) {
                    0 -> 1
                    else -> 0
                }
                movie_table.invalidate()
            }
        }
        movie_table.config.contentGridStyle = LineStyle().setColor(Color.TRANSPARENT)
        movie_table.config.isShowXSequence = false
        movie_table.config.isShowYSequence = false
        movie_table.tableData = tableData
    }

    override fun onDestroy() {
        initTitleBar(false)
        super.onDestroy()
    }

}